﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HospitalSystem.Core;
using System.Data.SqlClient;
using System.Data;
using System.Web.Configuration;
namespace HospitalSystem
{
    public partial class showcidinfo2 : System.Web.UI.Page
    {


        private show_Image _showimage;
        private findchildLog _fc;
        private GroupsLog _group;
        protected void Page_Load(object sender, EventArgs e)

        {

            if (Session["CID"] == null)
            {

                MessageBoxurl("กรุณากดค้นหา หรือลงทะเบียนเด็กก่อน", "findchild.aspx");

            }
            else
            {

                showsession();
                _showimage = new show_Image();
                _showimage.show_Images(cid_label, Image1);
                _fc = new findchildLog();
                _group = new GroupsLog();



            }
            //showrecord();
        }

        private void MessageBoxurl(string msg, string urld)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "');" + "window.location.href='" + urld + "';" + "</script>";
            Page.Controls.Add(lbl);
        }
        protected void showsession()
        {
            String ShowValue = (string)Session["CID"];
            cid_label.Text = ShowValue;
            String ShowValue2 = (string)Session["LastName"];
            lname.Text = ShowValue2;

            String ShowValue3 = (string)Session["FirstName"];
            fname.Text = ShowValue3;


            String ShowValue4 = (string)Session["CID"];
            SelectStat.Text = ShowValue4;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            checkcaseno();
            //_group.crepsnumber(HostID, StaffID);
            //_group.psnumbertolabel(psnumber);
            //sendsessionpsnumber();
            //sendsession();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Groups.aspx");
        }


        protected void sendsessionpsnumber()
        {
           


        }


        private void creps()
        {

            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            SqlCommand cmd = new SqlCommand("SELECT count([PSNumber])+1 as id FROM [ServiceProvisionVsPerson] where CID=@CID and CaseNo=@CaseNo", scon);
            cmd.Parameters.AddWithValue("@CID", Session["CID"]);
            cmd.Parameters.AddWithValue("@CaseNo", Session["CaseNo"]);
            scon.Open();
            object triggerCount = cmd.ExecuteScalar();



            if ((int)triggerCount == 0)
            {
                Session["PSNumber"] = '1';
            }

            else
            {
                Session["PSNumber"] = triggerCount;
                //psnumbertosessoin();
                

            }
            scon.Close();
        }





        public void psnumbertosessoin()
        {

            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            SqlCommand CmdSql47 = new SqlCommand("SELECT PSNumber FROM [ServiceProvision] WHERE PSNumber = (SELECT MAX(PSNumber)  FROM [ServiceProvision] where CID=@CID and CaseNo=@CaseNo)", scon);
            CmdSql47.Parameters.AddWithValue("@CID", Session["CID"]);
            CmdSql47.Parameters.AddWithValue("@CaseNo", Session["CaseNo"]);
            scon.Open();

            SqlDataReader reader = CmdSql47.ExecuteReader();
            // DataTable dt = new DataTable();
            //  dt.Load(reader);\
            // = reader;

            if (reader.Read())
            {
                Session["PSNumber"] = reader["PSNumber"].ToString();



            }
            else
            {

            }
            scon.Close();
            reader.Close();



        }










        protected void sendsession()
        {
            Session["CID"] = SelectStat.Text;
            Session["FirstName"] = fname.Text;
            Session["LastName"] = lname.Text;


            //_fc.Bindcase(SelectStat, caseno);


            //Session["CaseNo"] = caseno.Text;
            //String CaseNo = (string)Session["CaseNo"];
         creDetails();
            Response.Redirect("~/servicerecord2.aspx");



        }

        public void creDetails()
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            SqlCommand command22 = new SqlCommand("INSERT INTO [ServiceProvisionVsPerson] (CID,CaseNo,PSNumber,id) VALUES (@CID,@CaseNo,@PSNumber,@id)", scon);
            scon.Open();
            command22.Parameters.AddWithValue("@id", Session["id"]);
            command22.Parameters.AddWithValue("@CID", Session["CID"]);
            command22.Parameters.AddWithValue("@CaseNo", Session["CaseNo"]);
            command22.Parameters.AddWithValue("@PSNumber", Session["PSNumber"]);
  



            command22.ExecuteNonQuery();
            scon.Close();
        }




        //ส่งค่า psnumber id ไปที่ label
        public void psnumbertolabel()
        {

            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            SqlCommand CmdSql47 = new SqlCommand("SELECT id FROM [ServiceProvision] WHERE id = (SELECT MAX(id)  FROM [ServiceProvision])", scon);
            scon.Open();

            SqlDataReader reader = CmdSql47.ExecuteReader();
            // DataTable dt = new DataTable();
            //  dt.Load(reader);\
            // = reader;

            if (reader.Read())
            {
                Session["id"] = reader["id"].ToString();



            }
            else
            {

            }
            scon.Close();
            reader.Close();



        }




        protected void checkcaseno()
        {


            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            //ต้องการแก้เคสที่มีอยู่แล้ว
            scon.Open();
            SqlCommand cmd = new SqlCommand("SELECT CID from Evaluation where CID = @sj", scon);
            cmd.Parameters.AddWithValue("@sj", cid_label.Text);
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read() != false)
            {

                //สร้าง case no จากตาราง plan ให้มากที่สุด  Plannig
                BindDatacaseno();
                creps();
                // cre id
                crepsnumber();
                //ส่ง id
                psnumbertolabel();
            sendsessionpsnumber();
            sendsession();
              
            }

            else
            {

                MessageBox("เด็กรายได้ยังไม่ได้ทำการคัดกรองเราจะพาคุณไปหน้าทำการคัดกรอง", "cal.aspx");

            }
            reader.Close();

        }





        public void crepsnumber()
        {

            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            //SqlCommand command2 = new SqlCommand("INSERT INTO [ServiceProvision] (PSNumber,CaseNo,CID,HostID,StaffID,PSDateTime) VALUES(@PSNumber,@CaseNo,@CID,@HostID,@StaffID,@PSDateTime)", scon);
            SqlCommand command2 = new SqlCommand("INSERT INTO [ServiceProvision] (HostID,StaffID,PSDateTime) VALUES(@HostID,@StaffID,@PSDateTime)", scon);
            scon.Open();
          //  command2.Parameters.AddWithValue("@PSNumber", Session["PSNumber"]);
         //  command2.Parameters.AddWithValue("@CaseNo", Session["CaseNo"]);
          // command2.Parameters.AddWithValue("@CID", Session["CID"]);
            command2.Parameters.AddWithValue("@HostID", HostID.Text);
            command2.Parameters.AddWithValue("@StaffID", StaffID.Text);
            command2.Parameters.AddWithValue("@PSDateTime", DateTime.Now);

            command2.ExecuteNonQuery();
            scon.Close();
        }
        private void MessageBox(string msg, string urld)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "');" + "window.location.href='" + urld + "';" + "</script>";
            Page.Controls.Add(lbl);
        }


        protected void showrecord()
        {


            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection scon = new SqlConnection(constr);
            //ต้องการแก้เคสที่มีอยู่แล้ว
            scon.Open();
            SqlCommand cmd = new SqlCommand("SELECT a.[CaseNo] as caseno,a.[PSNumber] as ps,a.[CID] as cid,c.StaffID as staff,c.PSDateTime as time FROM [ServiceProvisionVsPerson] a join [ServiceProvision] c on c.id=a.id where CID=@CID", scon);
            cmd.Parameters.AddWithValue("@CID", cid_label.Text);

            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            showrec.DataSource = ds;
            showrec.DataBind();
            scon.Close();




        }


        protected void familylist_SelectedIndexChanged(object sender, GridViewPageEventArgs e)
        {
            showrec.PageIndex = e.NewPageIndex;
            showrecord();
        }

        protected void findstaffID()
        {



            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);

            SqlCommand CmdSql2 = new SqlCommand("SELECT Staff_id FROM [Employee] where Username=@Username", conn);
            conn.Open();
            CmdSql2.Parameters.AddWithValue("@Username", Session["idlogin"]);

            SqlDataReader reader2 = CmdSql2.ExecuteReader();



            if (reader2.Read())
            {
                StaffID.Text = reader2["Staff_id"].ToString();


            }
            else
            {

            }
            conn.Close();
            reader2.Close();

        }




         public void BindDatacaseno()
        {


            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn = new SqlConnection(constr);

            SqlCommand CmdSql2 = new SqlCommand("SELECT cid,max(CaseNo) as caseno FROM [Plannig]  where CID=@CID group by CID", conn);
            conn.Open();
            CmdSql2.Parameters.AddWithValue("@CID", Session["CID"]);

            SqlDataReader reader2 = CmdSql2.ExecuteReader();



            if (reader2.Read())
            {
                Session["CaseNo"] = reader2["caseno"].ToString();


            }
            else
            {

            }
            conn.Close();
            reader2.Close();
        }



    }
}